// The NSpring Framework for .NET
// Copyright (c) 2003, Jeffrey Varszegi
// All rights reserved.
// 
// Redistribution and use in source and binary forms, with or without modification, 
// are permitted provided that the following conditions are met:
// 
// - Redistributions of source code must retain the above copyright notice, this 
// list of conditions and the following disclaimer.
// 
// - Redistributions in binary form must reproduce the above copyright notice, this 
// list of conditions and the following disclaimer in the documentation and/or other 
// materials provided with the distribution.
// 
// - Neither the name of the NSpring project nor the names of its contributors may 
// be used to endorse or promote products derived from this software without 
// specific prior written permission from the copyright owner.
// 
// - No product derived from this software may be called "NSpring", nor may 
// "NSpring" appear in the name of such a product, without specific prior written 
// permission from the copyright owner.
// 
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 
// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 
// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 
// IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 
// INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 
// BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 
// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE 
// OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 
// OF THE POSSIBILITY OF SUCH DAMAGE. 

namespace NSpring.Logging {

using System;    
using System.Text;
using System.IO;

using NSpring.Logging.Common;

/// <summary>
/// <para>
/// Each TypeFormatter provides formatting logic for a specific data type, and may be set on 
/// DataFormatter instances using the AddTypeFormatter() method.  No TypeFormatter 
/// implementations are provided with this package; they provide the ability to easily extend the 
/// formatting abilities of the package without having to write an EventFormatter or a DataFormatter
/// from scratch.
/// </para>
/// <para>
/// The default data formatter types (FlatDataFormatter, TreeDataFormatter, and XMLDataFormatter)
/// don't depend (in fact, WON'T) on TypeFormatters for formatting either primitive types or the 
/// structures DateTime, Decimal, and TimeSpan.  In order to provide custom formatting for these types,
/// use custom format strings set via the *Format properties.
/// </para>
/// </summary>
public abstract class TypeFormatter {
    private DataFormatter dataFormatter                 = null;
    
    protected Encoding encoding                         = Encoding.UTF8;
    
    /// <summary>
    /// Gets the type formatted by this TypeFormatter
    /// </summary>
    public abstract Type SupportedType {
        get;
    }
    
    /// <summary>
    /// Gets a non-platform-specific output name for the data type
    /// supported by this instance
    /// </summary>
    public abstract string SupportedTypeDescriptor {
        get;
    }
    
    /// <summary>
    /// Provides implementation-specific logic for formatting data objects
    /// </summary>
    /// <param name="o">The data object to format</param>
    /// <returns>A string representation of the specified data object</returns>
    public abstract string Format(object o);
    
    /// <summary>
    /// Provides implementation-specific logic for formatting data objects
    /// </summary>
    /// <param name="o">The data object to format</param>
    /// <param name="sb">The StringBuilder to which to append the formatted object's data</param>
    /// <returns>A string representation of the specified data object</returns>
    public virtual void Format(object o, StringBuilder sb) {
        sb.Append(Format(o));
    }
    
    /// <summary>
    /// Gets and sets the encoding used to format data objects
    /// </summary>
    public Encoding Encoding {
        get {
            return encoding;
        }
        set {
            if (value == null) {
                throw new ArgumentNullException();
            }
            encoding = value;
        }
    }
    
    /// <summary>
    /// Indicates whether the output of the TypeFormatter should be included in the formatted
    /// event without being escaped.  This would allow, for instance, for inclusion of XML-formatted
    /// data wrapped in the output of the XMLEventFormatter class, without the formatted data's XML
    /// characters being escaped.
    /// </summary>
    public abstract bool IsPassThroughRequested {
        get;
    }
    
    /// <summary>
    /// Sets the DataFormatter that uses this instance.  Used to ensure that only
    /// one DataFormatter uses a given TypeFormatter, enabling a reduction in lock
    /// contention
    /// </summary>
    public DataFormatter DataFormatter {
        set {
            lock (this) {
                if (value == null) {
                    return;
                }
                if (dataFormatter != null) {
                    throw new InvalidOperationException("A TypeFormatter instance can only be used with one data formatter");
                }
                dataFormatter = value;
            }
        }
    }
    
    /// <summary>
    /// Performs a deep copy.  Used to ensure that no data formatters use the same TypeFormatter instances,
    /// so that thread safety can be provided without the use of synchronization.
    /// </summary>
    /// <returns></returns>
    public abstract TypeFormatter Copy();
    
}

}
